import axios from 'axios'
import store from '@/store'
import storage from 'store'
// import notification from 'ant-design-vue/es/notification'
import { VueAxios } from './axios'
import { message,Modal,notification } from 'ant-design-vue'
import { ACCESS_TOKEN } from '@/store/mutation-types'

// 创建 axios 实例
const request = axios.create({
  // API 请求的默认前缀
  // baseURL: process.env.VUE_APP_API_BASE_URL,
  baseURL:"",
  timeout: 6000 // 请求超时时间
})

// 异常拦截处理器
const errorHandler = (error) => {
  if (error.response) {
    const data = error.response.data
    // 从 localstorage 获取 token
    const token = storage.get(ACCESS_TOKEN)
    if (error.response.status === 403) {
      notification.error({
        message: 'Forbidden',
        description: data.message
      })
    }
    if (error.response.status === 401 && !(data.result && data.result.isLogin)) {
      notification.error({
        message: 'Unauthorized',
        description: '登录过期,请重新登录！'
      })
      if (token) {
        store.dispatch('Logout').then(() => {
          setTimeout(() => {
            window.location.reload()
          }, 1500)
        })
      }
    }
  }
  return Promise.reject(error)
}

// request interceptor
request.interceptors.request.use(config => {
  const token = storage.get(ACCESS_TOKEN)
  // 让每个请求携带自定义 token 请根据实际情况自行修改
  if (token) {
    config.headers['X-Access-Token'] = token
  }
  return config
}, errorHandler)

// response interceptor
request.interceptors.response.use((response) => {
  return response.data
}, errorHandler)


// 登录过期跳转
// axios.interceptors.response.use(response => {
//     let data = response.data;
//     if([10002].includes(data.code)){
//       message.error("登录已过期，即将跳转到登录页面")
//       router.push({ name: "login" }); // 跳转到登录页面
//     }
//     return response;
//   })




//返回值解构
axios.interceptors.response.use(response => {
    let data = response.data;
    if(data.code==0){
      return Promise.resolve(data);
    }else{
        message.error(data.msg)
      return new Promise(() => { });
    }
  }, err => {
    const { response } = err;
    if (err && response) {
      switch (response.status) {
          case 400:
            message.error('错误请求');
            break;
          case 401:
            message.error('未授权，请重新登录');
            break;
          case 403:
            message.error('用户为登陆！');
            router.push({ name: "login" ,query: { redirect: router.currentRoute.fullPath }}); // 跳转到登录页面
            break;
          case 404:
            message.error('请求错误,未找到该资源');
            break;
          case 405:
            message.error('请求方法未允许');
            break;
          case 408:
            message.error('请求超时');
            break;
          case 500:
            message.error('服务器端出错');
            break;
          case 501:
            message.error('网络未实现');
            break;
          case 502:
            message.error('网络错误');
            break;
          case 503:
            message.error('服务不可用');
            break;
          case 504:
            message.error('网络超时');
            break;
          case 505:
            message.error('http版本不支持该请求');
            break;
          default:
            message.error('连接错误'+error.response.status);
      }
    }
    return new Promise(() => { });
  })


  export function post(url, data, otherConfig) {
    const formData = new FormData();
    Object.keys(data).forEach((key) => {
        formData.append(key, data[key]);
    });
    return axios.post(url, formData, otherConfig);
  }

  export function get(url, data, otherConfig) {
    return axios.get(url, { params: data, ...otherConfig });
  }



const installer = {
  vm: {},
  install (Vue) {
    Vue.use(VueAxios, request)
  }
}

export default request

export {
  installer as VueAxios,
  request as axios
}
